package com.chixing.controller;

import com.chixing.service.LoginCustomerService;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/17/3:35
 * @Description:
 */
@Controller
public class LoginCustomerController {
    @Autowired
    private LoginCustomerService loginCustomerService;




    @GetMapping("/publickey1")
    @ResponseBody
    public ServerResponse resregister() {
        System.out.println("2453465356");

        return loginCustomerService.getPublicKey1();
    }

    @GetMapping("/encrypted1")
    @ResponseBody
    public ServerResponse password(@RequestParam("encrypted1") String encrypted) {
        System.out.println("密文: ");
        System.out.println(encrypted);

        String privateKey = loginCustomerService.getPrivateKey1();

        try {
            System.out.println("解密中.....");
            String decryptedBytes = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));
            System.out.println("解密后：" + decryptedBytes);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("login")
    @ResponseBody
    public ServerResponse Login(Long custTelno,String custPassword,HttpSession session){
        System.out.println("前端返回的密码："+custPassword);
        System.out.println("session id"+session.getId());
        ServerResponse serviceResponse = loginCustomerService.Login(custTelno,custPassword);

//        ModelAndView mav = new ModelAndView();

        System.out.println(serviceResponse);
        if(serviceResponse.getCode() == 200){ // 用户找到了，也就是 用户名和密码是正确的，跳转到首页index.jsp , 首页中显示用户名
            session.setAttribute("ServiceResponse",serviceResponse); // 绑定登录用户 到session中
//            mav.setViewName("index");   //
        }
//        else{ //  用户名和密码是错误的，跳转到login.jsp ，提示 用户名或密码错误，请重新输入
//            mav.addObject("loginMsg1","手机号码或密码错误，请重新输入");
//            mav.setViewName("customer/login"); //  /customer/login.jsp
//        }
        return serviceResponse;
    }

    //退出
    @GetMapping("logout")
    public String logout(HttpSession session){

        session.removeAttribute("ServiceResponse");// 退出： 移除session中的登录用户
        //System.out.println("235436476turhdgfgnhgvgffg");
        return "redirect:/index.jsp";  // 重定向(绝对路径)
    }


    @PostMapping("code")
    public ModelAndView getByEmail(@RequestParam String custEmail,HttpSession session){
        System.out.println("22222222222"+custEmail);
        System.out.println("session id" + session.getId());
        ServerResponse serverResponse = loginCustomerService.getByEmail(custEmail);
        ModelAndView mav = new ModelAndView();
        System.out.println(serverResponse);
        if(serverResponse.getCode() == 200){
            session.setAttribute("ServerResponse",serverResponse);//绑定登录用户到session中

        }else {//用户名和密码错误跳转到登录页面，提示 用户名和密码错误
            mav.addObject("loginMsg3","邮箱或密码错误，请重新输入");
            mav.setViewName("customer/login");
        }
        return mav;
    }

    @PostMapping("Validation")
    public  ModelAndView getByEmailAndPassword(String custEmail,Integer passwordEmail,HttpSession session){
        System.out.println("1111111111111");
        System.out.println("session id"+session.getId());
        ServerResponse serverResponse = loginCustomerService.getByEmailAndPassword(custEmail, passwordEmail);
        ModelAndView mav = new ModelAndView();
        System.out.println(serverResponse);
        if(serverResponse.getCode() == 200){
            session.setAttribute("ServerResponse",serverResponse);
            mav.setViewName("index");
        }else{
                mav.addObject("loginMsg3","验证码错误,请重新输入");
                mav.setViewName("customer/login");
            }
        return mav;

    }




}
